# -*- coding: utf-8 -*-
# @Time        :2025/4/7 17:28
# @Author      :文刀水寿
# @File        : views.py
"""
 @Description :路由 +  视图函数
"""
# 蓝图
from flask import Blueprint, request, render_template, jsonify, Response

# 可以管理多个蓝图
blue = Blueprint('user', __name__)


# blue2 = Blueprint('user2', __name__)


@blue.route('/')
def index():
    return 'index'


# 请求和响应
# 请求：


@blue.route('/request/', methods=['GET', 'POST'])
def get_request():
    pass
    # print(request)
    # 重要属性
    #
    # print(request.method)  # 请求方式 GET*POST
    #
    # # GET请求的参数
    # print(request.args)  # ImmutableMultiDict([('name', 'lisi'), ('name', 'wangwu'), ('age', '33')])
    # # ImmutableMultiDict 类字典对象 可以出现重复的key
    #
    # # 取元素
    # # print(request.args['name'], request.args['age']) # lisi 33
    # # print(request.args.get('name')) # lisi
    # # print(request.args.getlist('name')) # ['lisi', 'wangwu']
    #
    # # POST请求
    # print(request.form)  # ImmutableMultiDict([('name', 'lucky'), ('age', '33')])
    # print(request.form.get('name'))  # lucky
    #
    # # cookie
    # print(request.cookies)  # ImmutableMultiDict([])
    # print(request.path)  # /request/
    # print(request.url)  # http://127.0.0.1:5000/request/
    # print(request.base_url)  # http://127.0.0.1:5000/request/


@blue.route('/response/')
def get_response():
    pass
    # 返回字符串
    # return 'response ok!'

    # 模板渲染
    # return render_template('index.html', name='刘涛', age=22)

    # 返回json数据
    # data = {'name': 'liutao', 'age': 22}
    # return jsonify(data)

    # 自定义response对象
    html = render_template('index.html', name='刘涛', age=22)
    print(html)

    res = Response(html)
    return res

